﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FrontEnd_Dialog_EntryDetail : System.Web.UI.Page
{
    public String message = "";
    protected BLO.Entry entry;
    protected BLO.Category category;
    protected void Page_Load(object sender, EventArgs e)
    {
        getDataFromControl();
    }
    protected void getDataFromControl()
    {
        if (Session["LoginUser"] == null)
        {
            Response.Redirect("~/FrontEnd/Default.aspx");
            return;
        }
        string userid = Session["LoginUser"].ToString();

        if (Request.HttpMethod == "POST")
        {
            string action = Request.Form["action"];
            if (action == "ENTRY")
            {
                string title = Request.Form["entry_title"];
                string catogry = Request.Form["entry_category"];
                string content = Request.Form["entry_content"];
                string date = Request.Form["entry_date"];
                string view = Request.Form["entry_view"];
                string comment = Request.Form["entry_comment"];

                entry = new BLO.Entry();
                entry.Info.UserID = userid;
                entry.Info.Title = title;
                entry.Info.CategoryID = Convert.ToInt32(catogry);
                entry.Info.Content = content;
                entry.Info.Actived = (action == "POST") ? true : false;
                entry.Info.DatePosted = date;
                entry.Info.ViewPolicy = Convert.ToInt32(view);
                entry.Info.CommnentPolicy = Convert.ToInt32(comment);
                bool result = entry.Insert();
                if (result)
                {
                    Response.Write("true");
                }
            }
            else if (action == "CATEGORY")
            {
                string category_name = Request.Form["category_name"];
                category = new BLO.Category();
                category.Info.UserID = userid;
                category.Info.Name = category_name;
                int last_id = category.Insert();
                Response.Write(last_id.ToString());
            }
           
            Response.End();
        }
        else
        {
            category = new BLO.Category();
            List<DTO.Category> categorys =  BLO.Category.GetCategorysOfUser(userid);
            if (categorys.Count != 0)
            {
                entry_category.DataSource = categorys;
                entry_category.DataTextField = "Name";
                entry_category.DataValueField = "id";
                entry_category.DataBind();
            }
            else
            {
            }
        }

    }
}
